package com.hzz.framework.util;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

/**
 * 微信菜单创建
 * 
 * @author 78493
 *
 */
public class WechatUtil {
	private static final Logger logger = LoggerFactory.getLogger(WechatUtil.class);
	public static final String APPID = "wx0625e481751f5be1";
	public static final String SECRET = "c83f5e76ace4f21279f9336dd68fc914";
	public static final String TOKEN = "123456";
	public static final String AESKEY = "XTzDO0gfo6I4ydbe4dPK9yfNUP2LfDyvzjI3mdbI4zq";

	private static WxMpService getWxMpService() {
		WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
		config.setAppId(APPID); // 设置微信公众号的appid
		config.setSecret(SECRET); // 设置微信公众号的app
									// corpSecret
		config.setToken(TOKEN); // 设置微信公众号的token
		config.setAesKey(AESKEY); // 设置微信公众号的EncodingAESKey

		WxMpService wxService = new WxMpServiceImpl();
		wxService.setWxMpConfigStorage(config);
		return wxService;
	}

	/**
	 * 根据code获取微信用户信息
	 * 
	 * @param code
	 * @return
	 */
	public static WxMpUser getWechatInfo(String code) {
		try {
			WxMpService wxService = getWxMpService();
			// 根据code获取openId
			WxMpOAuth2AccessToken result = wxService.oauth2getAccessToken(code);
			logger.info(".getWechatInfo WxMpOAuth2AccessToken={}", result);
			if (result != null) {
				// 根据openId查询微信用户信息
				WxMpUser user = wxService.oauth2getUserInfo(result, null);
				logger.info(".getWechatInfo WxMpUser={}", user);
				return user;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		try {
			WxMpService wxService = getWxMpService();

			String domain = "http://hzz.cdhongyi.com:10011/";
			// String domain = "http://47.108.171.242:10011/";
			WxMenu menu = new WxMenu();
			List<WxMenuButton> buttons = new ArrayList<>();
			WxMenuButton button1 = new WxMenuButton();
			button1.setName("投诉举报");
			button1.setType(WxConsts.MenuButtonType.VIEW);
			// button1.setUrl("http://www.cjeye.net");
			button1.setUrl(domain + "wechat/complain/add");

			WxMenuButton button2 = new WxMenuButton();
			button2.setName("科普知识");
			button2.setType(WxConsts.MenuButtonType.CLICK);
			List<WxMenuButton> subButtons2 = new ArrayList<>();// 子菜单
			WxMenuButton subBtn2_1 = new WxMenuButton();
			subBtn2_1.setName("河湖档案");
			subBtn2_1.setType(WxConsts.MenuButtonType.VIEW);
			subBtn2_1.setUrl(domain + "wechat/riverList");
			subButtons2.add(subBtn2_1);
			WxMenuButton subBtn2_2 = new WxMenuButton();
			subBtn2_2.setName("知识园地");
			subBtn2_2.setType(WxConsts.MenuButtonType.VIEW);
			subBtn2_2.setUrl(domain + "wechat/noticeList");
			subButtons2.add(subBtn2_2);
			WxMenuButton subBtn2_3 = new WxMenuButton();
			subBtn2_3.setName("宣传引导");
			subBtn2_3.setType(WxConsts.MenuButtonType.VIEW);
			subBtn2_3.setUrl(domain + "wechat/publicityList");
			subButtons2.add(subBtn2_3);
			WxMenuButton subBtn2_4 = new WxMenuButton();
			subBtn2_4.setName("转载文章");
			subBtn2_4.setType(WxConsts.MenuButtonType.VIEW);
			subBtn2_4.setUrl(domain + "wechat/reprintedList");
			subButtons2.add(subBtn2_4);
			button2.setSubButtons(subButtons2);

			WxMenuButton button3 = new WxMenuButton();
			button3.setName("共享信息");
			button3.setType(WxConsts.MenuButtonType.CLICK);
			List<WxMenuButton> subButtons3 = new ArrayList<>();// 子菜单
			WxMenuButton subBtn3_1 = new WxMenuButton();
			subBtn3_1.setName("政策新闻");
			subBtn3_1.setType(WxConsts.MenuButtonType.VIEW);
			subBtn3_1.setUrl(domain + "wechat/policyList");
			subButtons3.add(subBtn3_1);
			WxMenuButton subBtn3_2 = new WxMenuButton();
			subBtn3_2.setName("镇级公众号");
			subBtn3_2.setType(WxConsts.MenuButtonType.VIEW);
			subBtn3_2.setUrl(domain + "wechat/gzhList");
			subButtons3.add(subBtn3_2);
			WxMenuButton subBtn3_3 = new WxMenuButton();
			subBtn3_3.setName("公众号二维码");
			subBtn3_3.setType(WxConsts.MenuButtonType.VIEW);
			subBtn3_3.setUrl(domain + "wechat/gzhImg");
			subButtons3.add(subBtn3_3);
			button3.setSubButtons(subButtons3);

			buttons.add(button1);
			buttons.add(button2);
			buttons.add(button3);
			menu.setButtons(buttons);
			String result = wxService.getMenuService().menuCreate(menu);
			System.out.println("WechatMenuController.create>>>>>>>>>" + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
